package fr.inria.phoenix.scenario.bwsnfirefighting.impl.context;

import fr.inria.diagen.core.ServiceConfiguration;
import fr.inria.diagen.core.exception.InvocationException;
import fr.inria.phoenix.scenario.bwsnfirefighting.framework.datatype.DangerLevel;
import fr.inria.phoenix.scenario.bwsnfirefighting.framework.datatype.Location;
import fr.inria.phoenix.scenario.bwsnfirefighting.framework.distributed.alertdetection.AlertDetection;
import fr.inria.phoenix.scenario.bwsnfirefighting.framework.distributed.alertdetection.ManualButtonProxy;

public class AlertDetectionImpl extends AlertDetection {
	
	public AlertDetectionImpl(ServiceConfiguration serviceConfiguration) {
		super(serviceConfiguration);
	}

	@Override
	public void onNewActivated(ManualButtonProxy proxy,
			Boolean newActivatedValue) {
		this.setAlertDetection(DangerLevel.ALERT, proxy.getLocation());

	}

	@Override
	public void onNewSmokeDetection(Boolean newSmokeDetectionValue,
			Location location) {
		try{
			if (newSmokeDetectionValue){
				if (this.getTemperatureDetection(location) == DangerLevel.SAFE){
					this.setAlertDetection(DangerLevel.WARNING, location);
				}
			}
			else{
				if (this.getTemperatureDetection(location) == DangerLevel.SAFE){
					this.setAlertDetection(DangerLevel.SAFE, location);
				}
				else if (this.getTemperatureDetection(location) == DangerLevel.WARNING){
					this.setAlertDetection(DangerLevel.WARNING, location);
				}
				else if (this.getTemperatureDetection(location) == DangerLevel.ALERT){
					this.setAlertDetection(DangerLevel.ALERT, location);
				}
			}
		} catch (InvocationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onNewTemperatureDetection(
			DangerLevel newTemperatureDetectionValue, Location location) {
		try {
			if (newTemperatureDetectionValue == DangerLevel.SAFE){
				if (this.getSmokeDetection(location)){
					this.setAlertDetection(DangerLevel.WARNING, location);
				}
				else{
					this.setAlertDetection(DangerLevel.SAFE, location);
				}
			
			}
			else if (newTemperatureDetectionValue == DangerLevel.WARNING){
				this.setAlertDetection(DangerLevel.WARNING, location);
			}
			else if (newTemperatureDetectionValue == DangerLevel.ALERT){
				this.setAlertDetection(DangerLevel.ALERT, location);
			}
		} catch (InvocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}



